// pages/components/upload/upload.js
import {
    uploadUrl,
    fileDomain,
    loadingTitle,
    cusFileDomain,
} from '../../../utils/env';
const picList = [];

Component({
    /**
     * 组件的属性列表
     */
    properties: {

        key: {
            type: String,
            value: '',
        },
        disable: {
            type: Boolean,
            value: false,
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        picList,
        fileDomain,
    },
    // ready() {
    //     if (this.properties.isCus) {
    //         this.setData({
    //             fileDomain: cusFileDomain,
    //         });
    //     }
    //     if (this.properties.key) {
    //         this.setData({
    //             picList: this.properties.key.split(',').map((item) => {
    //                 return {
    //                     localPath: this.data.fileDomain + item,
    //                     webKey: item,
    //                 };
    //             }),
    //         });
    //     }
    // },
    observers: {
        key() {
            //监听图片回填   
            console.log(key)
        },
    },
    /**
     * 组件的方法列表
     */
    methods: {
        async upload() {
            let that = this
            wx.chooseImage({
                count: 1,
                sizeType: ['compressed'],
                sourceType: ['camera'],
                success: (res) => {
                    wx.showLoading({
                        title: loadingTitle,
                    });
                    console.log(res)
                    wx.getFileSystemManager()
                        .readFile({ //读取本地文件内容
                            filePath: res.tempFilePaths[0], // 文件路径
                            encoding: 'base64', // 返回格式
                            success: ({ data }) => {
                                console.log(data)
                                var base64 = 'data:image/jpg;base64,' + data;
                                console.log(base64)
                                const picList = that.data.picList;
                                picList.push({
                                    localPath: base64,
                                    webKey: '',
                                });
                                that.setData({ picList });

                                that.triggerEvent('update', {
                                    res: picList.map((item) => item.localPath).toString(),
                                });
                                wx.hideLoading();
                            },
                            fail(res) {
                                console.log('fail', res)
                            }
                        })
                    // wx.request({
                    //     url: res.tempFilePaths[0],
                    //     method: 'GET',
                    //     responseType: 'arraybuffer',
                    //     success: function(res) {
                    //         var base64 = 'data:image/jpg;base64,' + wx.arrayBufferToBase64(res.data);
                    //         console.log(base64)
                    //         const picList = that.data.picList;
                    //         picList.push({
                    //             localPath: base64,
                    //             webKey: '',
                    //         });
                    //         that.setData({ picList });

                    //         that.triggerEvent('update', {
                    //             res: picList.map((item) => item.localPath).toString(),
                    //         });
                    //         wx.hideLoading();
                    //     },
                    //     complete: function(res) {
                    //         console.log(res)
                    //     }
                    // });
                    // wx.uploadFile({
                    //     url: uploadUrl,
                    //     filePath,
                    //     name: 'file',
                    //     header: {
                    //         'Content-Type': 'multipart/form-data',
                    //         Authorization: wx.getStorageSync('token'),
                    //     },
                    //     formData: {
                    //         name: `recsteel-${this.properties.module}-${+new Date()}`,
                    //     },
                    //     success: (res) => {
                    //         console.log('uploadSuccess', res);
                    //         wx.hideLoading();
                    //         const data = JSON.parse(res.data);
                    //         if (data.success) {
                    //             const picList = this.data.picList;
                    //             picList.push({
                    //                 localPath: filePath,
                    //                 webKey: data.data,
                    //             });
                    //             this.setData({ picList });
                    //             this.triggerEvent('update', {
                    //                 res: picList.map((item) => item.webKey).toString(),
                    //             });
                    //         } else {
                    //             wx.showToast({
                    //                 title: data.message,
                    //                 icon: 'none',
                    //             });
                    //         }
                    //     },
                    //     fail(e) {
                    //         wx.hideLoading();
                    //         console.log('uploaderr', e);
                    //     },
                    // });
                },
            });
        },
        previewImage(e) {
            const current = e.currentTarget.dataset.url;
            const urls = this.data.picList.map((item) => item.localPath);
            wx.previewImage({
                urls,
                current,
            });
        },
        deletePic(e) {
            this.data.picList.splice(e.currentTarget.dataset.index, 1);
            this.setData({
                picList: this.data.picList,
            });
            this.triggerEvent('update', {
                res: this.data.picList.map((item) => item.localPath).toString(),
            });
        },
    },
});